package com.pss.oauth2.impl;

import com.google.common.collect.Lists;
import com.pss.oauth2.dao.ApiRoleMapper;
import com.pss.oauth2.entity.ApiRole;
import com.pss.oauth2.service.ApiRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 资源与角色匹配关系管理业务类
 */
@Service
public class ResourceServiceImpl {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ApiRoleService apiRoleService;

    @PostConstruct
    public void initData() {
        List<ApiRole> list = apiRoleService.list();
        Map<String, List<String>> resourceRolesMap = list.parallelStream().collect(Collectors.toMap(ApiRole::getApiUrl, apiRole -> {
            String roles = apiRole.getRoles();
            return Lists.newArrayList(roles.split(","));
        }, (key1, key2) -> key1));
        redisTemplate.opsForValue().set("RESOURCE_ROLES_MAP", resourceRolesMap);
    }
}
